package com.example.springboot.controller;

import com.example.springboot.pojo.R;
import com.example.springboot.pojo.Student;
import com.example.springboot.pojo.vo.StudentVo;
import com.example.springboot.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @outhor fufeixiang
 * @date 2021-11-16
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    //查询学生列表
    @GetMapping("/list")
    public R findAll(){
        //查询所有学生
        List<Student> students = studentService.findAll();
        //返回
        return R.ok().data("list",students);
    }
    //条件带分页查询
    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable int page,@PathVariable int pageSize,@RequestBody(required = false) StudentVo studentVo){
        //开始分页查询
        PageInfo<Student> pageInfo = studentService.search(page,pageSize,studentVo);
        //返回
        return R.ok().data("total",pageInfo.getTotal()).data("rows",pageInfo.getList());
    }

    //添加学生
    @PostMapping("/add")
    public R addStudent(@RequestBody Student student){
        studentService.insert(student);
        return R.ok();
    }

    //修改学生
    @PostMapping("/update")
    public R updateStudent(@RequestBody Student student){
        studentService.update(student);
        return R.ok();
    }

    //删除学生
    @GetMapping("delete/{ids}")
    public R deleteStudent(@PathVariable Long[] ids){
        studentService.delete(ids);
        return R.ok();
    }



}
